package logic

import (
	"context"
	"fmt"
	"github.com/jinzhu/gorm"
	"teacher1/model/mysql"
	"teacher1/teacherrpc/internal/svc"
	"teacher1/teacherrpc/teacherrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type TeacherUpdatePwdLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewTeacherUpdatePwdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *TeacherUpdatePwdLogic {
	return &TeacherUpdatePwdLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *TeacherUpdatePwdLogic) TeacherUpdatePwd(in *teacherrpc.TeacherUpdatePwdRequest) (*teacherrpc.TeacherUpdatePwdResponse, error) {
	// todo: add your logic here and delete this line

	if in.Password != in.NewPassword {
		return nil, fmt.Errorf("两次输入的密码不一致，请重新输入")
	}
	teacher := mysql.Teacher{
		Model:    gorm.Model{ID: uint(in.TeacherId)},
		Password: in.NewPassword,
	}
	mysql.DB.Debug().Model(&teacher).Updates(&teacher)
	return &teacherrpc.TeacherUpdatePwdResponse{Id: int64(teacher.ID)}, nil
}
